/**
 *  JSimProd - Java simulation code for production processes
 *  Copyleft Jorge Mira Yagüe <jorge.mira.yague@gmail.com>
 *  Universidad de Alicante 2009
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package gui.tools.models;

import java.util.List;
import java.util.Vector;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import lib.plan.Item;

/**
 * This class is used as a model to show items
 * It shows the most relevant properties of the item as number, category and priority
 * It allows to add and remove items from the model, updating the view of the table
 * @see Item
 * @see JTable
 * @see TableModel
 * @see DefaultTableModel
 * @author Jorge Mira Yagüe
 */
public class TMItem extends DefaultTableModel {

    private List<Item> items;

    /**
     * Class builder
     * @param items The item list to show in the table
     */
    public TMItem(List<Item> items) {
        super();

        columnIdentifiers.add("Number");
        columnIdentifiers.add("Category");
        columnIdentifiers.add("Priority");
        this.setItems(items);
    }

    public List<Item> getItems() {
        return items;
    }

    public void setItems(List<Item> items) {
        this.items = items;
        refresh();
    }

    public Item getItem(int index) {
        return items.get(index);
    }

    public void setItem(int index, Item item) {
        items.set(index, item);
        refresh();
    }

    public void addItem(Item item) {
        items.add(item);
        refresh();
    }

    public void removeItem(int index) {
        items.remove(index);
        refresh();
    }

    private void refresh() {
        dataVector = new Vector();
        Vector<String> v;
        for (Item i : items) {
            v = new Vector();
            v.add(String.valueOf(i.getNumber()));
            v.add(i.getCategory());
            v.add(String.valueOf(i.getPriority()));
            dataVector.add(v);
        }
    }

    /**
     * This function makes the cells not editables by the user
     * @param x
     * @param y
     * @return always false
     */
    @Override
    public boolean isCellEditable(int x, int y) {
        return false;
    }
}
